#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QString>
#include <QMessageBox>
#include <QFileDialog>
#include "RipRoute.h"
#include <cstdio>
//#include <thread>

const QString graphFileName = "netGraphic.txt";
const QString routeFileName = "routes.txt";

//QString MainWindow::qstrGraphName="";
//QString MainWindow::qstrRouteName="";

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
  , ui(new Ui::MainWindow)
  , qstrGraphName()
  , qstrRouteName()
  , bFirstRun(true)
{
    ui->setupUi(this);

    ui->cbGraph->clear();
    ui->cbRoute->clear();

    connect(ui->btnGraph, &QAbstractButton::clicked, this, &MainWindow::onBnGraphClicked);
    connect(ui->btnRoute, &QAbstractButton::clicked, this, &MainWindow::onBnRouteClicked);
    connect(ui->btnNextStep, &QAbstractButton::clicked, this, &MainWindow::onBnNextStepClicked);
    connect(ui->btnRunAll, &QAbstractButton::clicked, this, &MainWindow::onBnRunAllClicked);

    char *nextlogfile = "route_next.log";
    remove(nextlogfile);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onBnGraphClicked()
{
    QString fileDirPath = QFileDialog::getOpenFileName(this,tr("file"), "", tr("文本文件(*txt)"));
    qDebug()<<"File Path:"<<fileDirPath;

    QFileInfo fileinfo = QFileInfo(fileDirPath);
    qstrGraphName = fileinfo.fileName();
    qDebug()<<"qstrGraphName:"<<qstrGraphName;

    if(!qstrGraphName.isNull() && qstrGraphName.compare(graphFileName)==0)
    {
        ui->cbGraph->clear();
        ui->cbGraph->addItem(fileDirPath);
    }
    else
    {
        QMessageBox::information(nullptr, tr("Error"), tr("拓扑图选择错误!"));
    }
}

void MainWindow::onBnRouteClicked()
{
    QString fileDirPath = QFileDialog::getOpenFileName(this,tr("file"), "", tr("文本文件(*txt)"));
    qDebug()<<"File Path:"<<fileDirPath;

    QFileInfo fileinfo = QFileInfo(fileDirPath);
    qstrRouteName = fileinfo.fileName();
    qDebug()<<"qstrRouteName:"<<qstrRouteName;

    if(!qstrRouteName.isNull() && qstrRouteName.compare(routeFileName)==0)
    {
        ui->cbRoute->clear();
        ui->cbRoute->addItem(fileDirPath);
    }
    else
    {
        QMessageBox::information(nullptr, tr("Error"), tr("路由表选择错误!"));
    }
}

void MainWindow::onBnNextStepClicked()
{
    if(qstrGraphName.isNull())
    {
        QMessageBox::information(nullptr, tr("Error"), tr("拓扑图未选择!"));
        return;
    }
    if(qstrRouteName.isNull())
    {
        QMessageBox::information(nullptr, tr("Error"), tr("路由表未选择!"));
        return;
    }

    runNext();
}

void MainWindow::onBnRunAllClicked()
{
    if(qstrGraphName.isNull())
    {
        QMessageBox::information(nullptr, tr("Error"), tr("拓扑图未选择!"));
        return;
    }
    if(qstrRouteName.isNull())
    {
        QMessageBox::information(nullptr, tr("Error"), tr("路由表未选择!"));
        return;
    }

    runAll();
}

void MainWindow::runNext()
{
    RipRoute route(qstrGraphName.toStdString(),qstrRouteName.toStdString(),true);
    if(bFirstRun)
    {
        route.printGraphRoute();
        bFirstRun = false;
    }

    route.RunNext();
}

void MainWindow::runAll()
{
    RipRoute route(qstrGraphName.toStdString(),qstrRouteName.toStdString(),false);
    route.RunAll();
}
